# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :information.py
# @Time      :2022/7/31 19:34
# @Author    :吕华龙
from flask_restful import Api, Resource, reqparse, marshal
from flask import Blueprint, jsonify
from models.model import *
from models.fields import *
from flask_sqlalchemy import Pagination

infor_bp = Blueprint("infor_bp", __name__, url_prefix="/infor")

api = Api(infor_bp)


# 资讯视图
class InformationView(Resource):
    # 获取资讯列表
    def get(self):
        # 获取要展示第几页的数据
        resp = reqparse.RequestParser()
        resp.add_argument("page", type=int)
        args = resp.parse_args()
        # 资讯的全部数据获取到
        infor: Pagination = InformationModel.query.paginate(args['page'], 3, error_out=False)
        # 当前页的全部数据
        items = infor.items
        print("”“”“”“”“--+--”“”“”“”“”", items)
        # 表中总共有多少数据
        total = infor.total
        # 当前页数
        page = infor.page
        print("-------", infor, items)
        # 将数据序列化
        infor_marshal = marshal(items, infor_fields)
        # 构建数据格式
        list_1 = {
            "total": total,
            "page": page,
            "list": infor_marshal
        }
        # 返回响应
        return jsonify({'code': 200, 'msg': '获取资讯列表成功', 'data': list_1})


# 资讯
class InforInfoView(Resource):
    # 获取资讯详情
    def get(self):
        resp = reqparse.RequestParser()
        resp.add_argument("id", type=int)
        args = resp.parse_args()
        # 获取此资讯
        info = InformationModel.query.filter(InformationModel.id == args['id']).first()
        # 判断此资讯是否存在
        if not info:
            return jsonify({'code': 400, 'msg': '没有此资讯'})
        list_1 = {
            "id": info.id,
            "thumb_img": info.thumb_img,
            "title": info.title,
            "description": info.description,
            "click_rate": info.click_rate,
            "created_at": info.created_at
        }
        # 返回数据
        return jsonify({'code': 200, 'msg': '获取资讯详情成功', 'data': list_1})


# 资讯首页推荐
class InforInfoRemmendView(Resource):
    def get(self):
        # 获取全部资讯
        information = InformationModel.query.all()
        # infor_marshal = marshal(information, infor_fields)
        # print(infor_marshal)
        print(">>>>>>>>>>", information)
        list_1 = []
        # 循环数据，拿出click_rate大于1的数据
        for i in information:
            print("--------------", i)
            # 判断点击率大于1的获取出来
            if i.click_rate > 1:
                list_1.append({
                    "id": i.id,
                    "thumb_img": i.thumb_img,
                    "title": i.title,
                    "description": i.description,
                    "click_rate": i.click_rate,
                    "created_at": i.created_at
                })
        return jsonify({'code': 200, 'msg': '获取成功', 'data': list_1})


# 资讯分类
class InforClassify(Resource):
    def get(self):
        # 获取到全部数据
        infor = InformationModel.query.all()
        # 循环数据
        list_1 = []
        list_2 = []
        list_3 = []
        for i in infor:
            # 按资讯点击率分类为1 的分类，为2的分类
            if i.click_rate > 1:
                list_1.append({
                    "id": i.id,
                    "thumb_img": i.thumb_img,
                    "title": i.title,
                    "description": i.description,
                    "click_rate": i.click_rate,
                    "created_at": i.created_at
                })
            if i.click_rate < 2:
                list_2.append({
                    "id": i.id,
                    "thumb_img": i.thumb_img,
                    "title": i.title,
                    "description": i.description,
                    "click_rate": i.click_rate,
                    "created_at": i.created_at
                })
        # list_3.append(list_1)
        # list_3.append(list_2)
        return jsonify({'code': 200, 'msg': '分类资讯成功', 'list_1': list_1, "list_2": list_2})


# 创建路由
# 获取资讯列表数据
api.add_resource(InformationView, "/information")
# 咨询详情
api.add_resource(InforInfoView, "/infor_info")
# 获取首页推荐
api.add_resource(InforInfoRemmendView, "/infor_remmend")
# 资讯分类
api.add_resource(InforClassify, "/infor_classify")
